<template>
	<view class="bg-white">
		<view class="cu-bar solid-bottom">
			<view class="action flex flex-sub justify-center text-primary">
				<navigator :url="'../../index/peizhuang/index?type=' + type" hover-class="none">
					<view class="padding-lr">
						<text class="cuIcon-add"></text>
						<text>创建{{ typeName }}</text>
					</view>
				</navigator>
				<navigator :url="'../../index/jiadian/list/index?type=' + typeHall" hover-class="none">
					<view class="padding-lr">
						<text class="cuIcon-discover"></text>
						<text class="margin-left-xs">{{ typeName }}大厅</text>
					</view>
				</navigator>
			</view>
		</view>
		<view class="bg-white padding-lr" v-if="items.length > 0">
			<app-list-item sign="sandbox" :item="item" v-for="(item, index) in items" :key="index" @handleHref="handleHref" @handleShare="handleShare" :showActions="true" solidStyle="solid-bottom padding-bottom"></app-list-item>
		</view>
	</view>
</template>

<script>
	import { handleRequest } from '@/common/request.js';
	import cuCustom from '@/libs/colorui/components/cu-custom.vue';
	import appListItem from '@/components/app-list-item.vue';
	
	const navigationTitles = {
		'1': '配装',
		'2': '加点',
		'3': '技能'
	}
	
	export default {
		components: {
			cuCustom,
			appListItem
		},
		data() {
			return {
				type: 2,
				typeHall: 2,
				items: []
			}
		},
		computed: {
			typeName() {
				return navigationTitles[this.typeHall];
			},
			navigationTitle() {
				return navigationTitles[this.typeHall] + '模拟';
			}
		},
		onReady() {
			this.handleLoadData();
		},
		methods: {
			async handleLoadData() {
				let page = 1;
				
				if (this.isLoading) {
					return;
				}
				
				this.isLoading = true;
			
				await handleRequest({
					cmd: 'getpersonlistbytype',
					nType: this.typeHall,
					nUid: uni.getStorageSync('uid') || 0,
					nPage: page
				}, false).then(async res => {
					this.isLoading = false;
					this.items = res.map(item => {
						item.avatar = 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10006.jpg';
						
						// 存储到本地
						uni.setStorageSync(`nIndex:${this.typeHall}:${item.nIndex}`, item);
						
						return item;
					});
				})
			},
			handleHref(nIndex) {
				uni.navigateTo({
					url: `../../index/jiadian/index?nIndex=${nIndex}`
				})
			},
			handleShare(item) {
				uni.showModal({
					title: '提示',
					content: '确认要分享到大厅吗',
					success: async (res) => {
						if (res.confirm) {
							await handleRequest({
								cmd: 'sharetohall',
								nType: this.typeHall,
								nUid: uni.getStorageSync('uid'),
								nIndex: item.nIndex
							}, true).then(async res => {
								if (res.responseCode === '100') {
									uni.showToast({
										title: '分享成功',
										success: () => {
											this.showModal = false;
										}
									});
								} else {
									uni.showToast({
										icon: 'error',
										title: res.messageList[0].message,
										success: () => {}
									});
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style>

</style>
